package com.exam.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.entity.ApiResult;
import com.exam.entity.Teacher;
import com.exam.service.TeacherService;
import com.exam.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.annotation.Target;

@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 分页查询教师信息
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/{page}/{size}")
    public ApiResult findAll(@PathVariable Integer page, @PathVariable Integer size){
        Page<Teacher> teacherPage = new Page<>(page,size);
        IPage<Teacher> teacherIPage = teacherService.findAll(teacherPage);

        return ApiResultHandler.buildApiResult(200,"查询所有教师",teacherIPage);
    }

    /**
     * 根据教师ID查询教师信息
     * @param teacherId
     * @return
     */
    @GetMapping("/{teacherId}")
    public ApiResult findById(@PathVariable("teacherId") Integer teacherId){
        return ApiResultHandler.success(teacherService.findById(teacherId));
    }


    /**
     * 根据教师ID删除教师信息
     * @param teacherId
     * @return
     */
    @DeleteMapping("/{teacherId}")
    public ApiResult deleteById(@PathVariable("teacherId") Integer teacherId){
        return ApiResultHandler.success(teacherService.deleteById(teacherId));
    }



    /**
     * 更新教师信息
     * @param teacher
     * @return
     */
    @PutMapping
    public ApiResult update(@RequestBody Teacher teacher){
        return ApiResultHandler.success(teacherService.update(teacher));
    }


    /**
     * 添加教师信息
     * @param teacher
     * @return
     */
    @PostMapping
    public ApiResult add(@RequestBody Teacher teacher){
        if (teacher.getTeacherName() == null || teacher.getInstitute() == null || teacher.getSex() == null||
                teacher.getTel() == null ||  teacher.getPwd() == null ||
                teacher.getCardId() == null || teacher.getType() == null
        ){
            return ApiResultHandler.buildApiResult(500,"信息不能为空",null);
        }
       teacher = teacherService.add(teacher);
        if (teacher != null){
            return ApiResultHandler.buildApiResult(200,"添加该用户",teacher);
        }
        return ApiResultHandler.buildApiResult(500,"已经存在该用户",null);
    }

    /**
     * 找回密码功能
     * @param teacher
     * @return
     */
    @PostMapping("/editPassword")
    public ApiResult editPassword(@RequestBody Teacher teacher){
        if (teacher.getTeacherName() == null || teacher.getInstitute() == null || teacher.getSex() == null||
                teacher.getTel() == null ||  teacher.getPwd() == null || teacher.getEmail() == null ||
                teacher.getCardId() == null || teacher.getType() == null
        ){
            return ApiResultHandler.error(500,"信息不能为空");
        }
        int code = teacherService.editPassword(teacher);
        if (code != 200){
            return ApiResultHandler.error(500,"该用户不存在");
        }
        return ApiResultHandler.buildApiResult(200,"修改密码成功",null);
    }
}